package com.ml.mall.service.sys.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.sys.SysUserRoleNewDto;
import com.ml.mall.bean.req.sys.SysUserRoleQueryDto;
import com.ml.mall.bean.vo.sys.SysUserRoleVO;
import com.ml.mall.entity.sys.SysRoleUser;
import com.ml.mall.mapper.sys.SysRoleUserMapper;
import com.ml.mall.service.sys.ISysRoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author kyyang
 * Created 2021-01-06
 */
@Service
public class SysRoleUserServiceImpl extends ServiceImpl<SysRoleUserMapper, SysRoleUser> implements ISysRoleUserService {

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Override
    public List<SysRoleUser> listByUserId(long id) {
        return sysRoleUserMapper.selectByUserId(id);
    }

    @Override
    public List<SysUserRoleVO> sysUserRole(SysUserRoleQueryDto sysUserRoleQueryDto) {
        return sysRoleUserMapper.selectUserRole(sysUserRoleQueryDto);
    }

    @Override
    @Transactional
    public int sysUserRoleNew(SysUserRoleNewDto sysUserRoleNewDto) {
        int i = 0;
        Integer roleId = sysUserRoleNewDto.getRoleId();

        SysRoleUser s = new SysRoleUser();
        s.setUserId(sysUserRoleNewDto.getUserId());
        s.setRoleId(roleId);
        SysRoleUser st = sysRoleUserMapper.selectByKey(roleId, sysUserRoleNewDto.getUserId());
        if (st == null) {
            i = sysRoleUserMapper.insert(s);
        }
        return i;
    }

    @Override
    @Transactional
    public int sysUserRoleDel(SysUserRoleNewDto sysUserRoleNewDto) {
        Integer roleId = sysUserRoleNewDto.getRoleId();
        return sysRoleUserMapper.deleteByKey(sysUserRoleNewDto.getUserId(), roleId);
    }
}
